<?php

namespace src\DataBase;
class Config
{
    // 服务器地址
    public $hostname = '192.168.149.128';
    // 数据库名
    public $database = '';
    // 用户名
    public $username = 'root';
    // 密码
    public $password = '';
    // 端口
    public $hostport = '3306';
    // 数据库编码默认采用utf8
    public $charset = 'utf8';
    // 数据集返回类型
    public $resultset_type = 'array';
    // 时间字段取出后的默认时间格式
    public $datetime_format = 'Y-m-d H:i:s';


    public function __construct($hostname, $database, $username, $password, $charset)
    {
        if (empty($this->hostname) && !empty($hostname)) {
            $this->hostname = $hostname;
        }
        $this->database = $database;
        if (empty($this->username) && !empty($username)) {
            $this->username = $username;
        }
        $this->password = $password;
        $this->charset = $charset;
    }

    //连接数据库
    public function connectDb()
    {
        $this->conn = mysqli_connect($this->hostname, $this->username, $this->password);
        return $this->conn;
    }

    //设置字符集
    public function characterSet($conn)
    {
        $sql = 'set names ' . ($this->charset);
        $this->set = mysqli_query($conn, $sql);
        return $this->set;
    }

    //选择数据库
    public function selectDb($conn)
    {
        $this->selectdb = mysqli_select_db($conn, $this->database);
        return $this->selectdb;
    }

    //初始化数据库
    public function init()
    {
        $this->conn = $this->connectDb();
        if (!($this->conn)) {
            echo $this->msg = '数据库连接失败!';
        } else {
            $this->selectdb = $this->selectDb($this->conn);
            if ($this->selectdb) {
                $this->set = $this->characterSet($this->conn);
                if (!($this->set)) {
                    echo $this->msg = '字符集设置失败!';
                } else {
                    return $this->conn;
                }
            } else {
                echo $this->msg = '选择数据库失败!';
            }
        }
    }

    /*数组转为字符串
    $data = new DataBaseOperate('139.196.124.180','school','user1','Mcx857264','utf8');
    多字符插入:var_dump($data->arrayToString(array(1,2,3,4)));   =>   1,2,3,4
    echo "</br>";
    单条件查询:var_dump($data->where(array('studentNumber'=>'s1601081128')))  =>  studentNumber='s1601081128'
    echo "</br>";
    多条件查询:var_dump($data->where(array('studentNumber'=>'s1601081128','Id'=>'12')))  => studnetNumber='s1601081128'
    and Id='12';

    */
    public function arrayToString(array $data)
    {
        $count = count($data);

        if ($count > 1) {
            $keyString = join(',', array_keys($data));
            if (str_word_count($keyString) > 0) {
                $arrayKey = array_keys($data);
                $arrayValues = array_values($data);
                for ($i = 0; $i < $count; $i++) {
                    $stringKey = $arrayKey[$i];
                    $stringValues = sprintf("'%s'", $arrayValues[$i]);
                    if ($i < $count - 1) {
                        $stringResult .= ($stringKey . ' = ' . $stringValues . ' and ');
                    } else {
                        $stringResult .= ($stringKey . ' = ' . $stringValues);
                    }
                }
                return $stringResult;

            } else {
                $values = sprintf("%s", join(',', array_values($data)));
                return $values;
            }
        } else {
            $keyString = join(',', array_keys($data));
            if (str_word_count($keyString) > 0) {
                $values = sprintf("'%s'", join('', array_values($data)));
                return $keyString . ' = ' . $values;
            } else {
                $values = sprintf("%s", join('', array_values($data)));
                return $values;
            }
        }
    }
}

?>